// ProcessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VISUAL ASM.h"
#include "ProcessDlg.h"


// CProcessDlg dialog

IMPLEMENT_DYNAMIC(CProcessDlg, CDialog)

CProcessDlg::CProcessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProcessDlg::IDD, pParent)
{	
}

CProcessDlg::~CProcessDlg()
{
}


void CProcessDlg::FillListBox(CList<CString>& lstProcess){
	while(m_lstProcessName.GetCount())
		m_lstProcessName.DeleteString(0);
	POSITION pos = lstProcess.GetHeadPosition(); 
	while(pos != lstProcess.GetTailPosition()){
		CString str = lstProcess.GetNext(pos);
		m_lstProcessName.AddString(str);
	}
	this->UpdateData(FALSE);
}

void CProcessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_lstProcessName);
}


BEGIN_MESSAGE_MAP(CProcessDlg, CDialog)
	ON_BN_CLICKED(IDOK, &CProcessDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CProcessDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON1, &CProcessDlg::OnBnClickedButton1)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CProcessDlg message handlers

void CProcessDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CProcessDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CProcessDlg::OnBnClickedButton1()
{
	this->SetTimer(1, 5000, NULL);
	::SendMessage(this->GetSafeHwnd(), WM_TIMER, NULL, NULL);
	// TODO: Add your control notification handler code here
}

void CProcessDlg::OnTimer(UINT_PTR nIDEvent)
{
	lstProcess.RemoveAll();
	// TODO: Add your message handler code here and/or call default
	DWORD aProcesses[1024], cbNeeded, cProcesses;
	unsigned int i;
	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
		return;
	// Calculate how many process identifiers were returned.
	cProcesses = cbNeeded / sizeof(DWORD);
	// Print the name and process identifier for each process.
	for ( i = 0; i < cProcesses; i++ ){
		if( aProcesses[i] != 0 ){
			DWORD processID = aProcesses[i];
			TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
			// Get a handle to the process.
			HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |	PROCESS_VM_READ, FALSE, processID );
			// Get the process name.
			if (NULL != hProcess)
			{
				HMODULE hMod;
				DWORD cbNeeded;

				if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) ){
					GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
					CString str = szProcessName;
					lstProcess.AddTail(str);
				}
			}
			CloseHandle( hProcess );
		}
	}
	this->FillListBox(lstProcess);

	CDialog::OnTimer(nIDEvent);
}
